package com.neo.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.neo.util.ReadFromFile;


@Service
public class TempTransferService {

    protected static Logger logger= LoggerFactory.getLogger(TempTransferService.class); 
    

	
	/**
	 * 将csv文件2个字段过滤去重后重新生成csv
	 * @throws Exception
	 */
	public void transfer() throws Exception{
		Map<String,String> map = readMapFileByLines("D:\\点金石\\新债权信息\\还款流水对冲.csv");
		writeStringToFile(map,"D:\\点金石\\新债权信息\\temp.csv");
    	
	}
	
	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static Map readMapFileByLines(String fileName) {
		Map map = new HashMap();
		List<String[]> list = new ArrayList();
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容，一次读一整行：");
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "gbk"));
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				//System.out.println("line " + line + ": " + tempString);
				String[] str = tempString.split(",");
				list.add(str);
				map.put(str[0]+","+str[1], tempString);
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return map;
	}
	
    public static void writeStringToFile(Map<String,String> res, String name) throws IOException {

        try {
            File file = new File(name);
            //PrintStream ps = new PrintStream(new FileOutputStream(file));
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            for (String key : res.keySet()) {
            	
            	String str = res.get(key);
            	str += "\n";
                // ps.println(str);// 在已有的基础上添加字符串
                byte[] bs = str.getBytes("GBK");
                fileOutputStream.write(bs);
            }
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

	public static void main(String[] args) throws Exception {
		new TempTransferService().transfer();
	}

}
